const express = require("express");
const router = express.Router();
const { conMysql } = require("../db");


//一个字段可以用多个条件查询 searchval  查询编号或者姓名
router.get("/querySalesperson", (req, res) => {
    let { searchval = "", page = 1, pageSize = 10 } = req.query;
    //根据uid、uname 模糊查询业务员信息
    //const sql = `select * from db_salesperson where uid like '%${uid}%' and uname like '%${uname}%'`;
    const sql = `SELECT * FROM db_salesperson WHERE (uid LIKE '%${searchval}%' OR uname LIKE '%${searchval}%') LIMIT ${(page - 1) * pageSize},${pageSize};`
    conMysql(sql)
        .then((result) => {
            //查询所有的业务员的总条数
            const sql = `select count(*) as count from db_salesperson  WHERE (uid LIKE '%${searchval}%' OR uname LIKE '%${searchval}%');`;
            conMysql(sql)
                .then((result2) => {
                    res.output("查询成功", 0, {
                        list: result,
                        count: result2[0].count,
                    });
                })
                .catch((err) => {
                    console.log(err);
                    res.output(err, 500, null);
                });
        })
        .catch((err) => {
            console.log(err);
            res.output(err, 500, null);
        });

});

module.exports = router;